//
//  WBTabBar.m
//  MyWeiBo
//
//  Created by wangneihan on 16/1/9.
//  Copyright © 2016年 wangneihan. All rights reserved.
//

#import "WBTabBar.h"
#import "WBTabBarButton.h"
#import "ISPPersonalController.h"

@interface WBTabBar()

@property(nonatomic,weak)UIButton *plusButton;

@end


@implementation WBTabBar

-(void)setBtnItems:(NSArray *)btnItems
{
    _btnItems = btnItems;
    for (UITabBarItem *item in _btnItems) {
        WBTabBarButton *barBtn = [[WBTabBarButton alloc]init];
        barBtn.item = item;
        [self addSubview:barBtn];
    }
}

-(UIButton *)plusButton
{
    if(_plusButton == nil)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_background_icon_add"] forState:UIControlStateHighlighted];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [btn addTarget:self action:@selector(changeRoot) forControlEvents:UIControlEventAllTouchEvents];
        
        [btn sizeToFit];
        _plusButton = btn;
        
        [self addSubview:_plusButton];
    }
    return  _plusButton;
}

-(void)changeRoot
{
    self.window.rootViewController = [[ISPPersonalController alloc]init];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat iconH = self.bounds.size.height;
    CGFloat iconW = self.bounds.size.width/(self.items.count + 1);
    CGFloat iconX = 0;
    CGFloat iconY = 0;
    int x=0;
    for (UIView *subView in self.subviews) {
        //NSLog(@"%@",[subView class]);
        if([subView isKindOfClass:NSClassFromString(@"UIButton")])
        {
            NSLog(@"%d",x);
            iconX = iconW * x;
            if(x == 1) x = 2;
            subView.frame = CGRectMake(iconX, iconY, iconW, iconH);
            [self addSubview:subView];
            x++;
        }
    }
    
    self.plusButton.center = CGPointMake(self.bounds.size.width * 0.5, iconH * 0.5);
}

@end
